<?php

function ValidateFormData(&$frm, &$msg)
{
	$tempmsg = "";
	$count = count($frm);
	
	for($i=0;$i<$count;$i++) 
	{
		if ($i<$count && $frm[$i]["validation"]=="IsPassword") 
		{ 
			if ($frm[$i+1]["validation"]=="IsPassword" && ($frm[$i]["value"]!=$frm[$i+1]["value"] || $frm[$i]["value"]=="")) 
			{
				$tempmsg .= "- " . $frm[$i]["validationmsg"] . "<br>";
 				$frm[$i]["label"] = "<span class=\"wrong\">" . $frm[$i]["label"] . "</span>";
 				$frm[$i+1]["label"] = "<span class=\"wrong\">" . $frm[$i+1]["label"] . "</span>";
				$frm[$i]["value"]="";
				$frm[$i+1]["value"]="";
			}
		}
		elseif($frm[$i]["type"]=="file")
		{
			if($frm[$i]["validation"]=="IsNotEmpty"){
				
				if (!is_uploaded_file($frm[$i]["value"])){
					$tempmsg .= "- " . $frm[$i]["validationmsg"] . "<br>";
 					$frm[$i]["label"] = "<span class=\"wrong\">" . $frm[$i]["label"] . "</span>";
					
				}
			}	
			else
			{	
				continue;
				
			}	
			
			
		}
		elseif (!chk($frm[$i]["value"],$frm[$i]["validation"])) 
		{
				$tempmsg .= "- " . $frm[$i]["validationmsg"] . "<br>";
 				$frm[$i]["label"] = "<span class=\"wrong\">" . $frm[$i]["label"] . "</span>";
		}
	}
	
	if ($tempmsg!="") 
	{
		 $msg = "Data can not be saved because the form was not properly filled out:<br>$tempmsg";
		 return FALSE;
	}
	
	return TRUE;
}

function chk($val, $rule)
{
	$aRule=explode(",",$rule);
	
	foreach ($aRule as $rule) {
		switch ($rule) 
		{
			case "IsEmail":
				$cond=IsEmail($val);
				break;
			case "IsNumber":
				$cond=IsNumber($val);
				break;
			case "IsNotEmpty":
				$cond=IsNotEmpty($val);
				break;
			case "IsMoney":
				$cond=IsMoney($val);
				break;
			case "IsPercent":
				$cond=IsPercent($val);
				break;
			case "IsUSPhone":
				$cond=IsUSPhone($val);
				break;
			case "IsASTPhone":
				$cond=IsASTPhone($val);
				break;
			case "IsAreaCode":
				$cond=IsAreaCode($val);
				break;
			case "IsPostCode":
				$cond=IsPostCode($val);
				break;
			case "IsHTTP":
				$cond=IsHTTP($val);
				break;
			case "IsIP":
				$cond=IsIP($val);
				break;
			case "IsSelected":
				$cond=IsSelected($val);
				break;
			case "IsColor":
				$cond=IsColor($val);
				break;
			default:
				$cond=TRUE;
		}
		if (!$cond)
			return FALSE;
	}
	return TRUE;
}

//Form validation functions
function IsHTTP($addr)
{
	$valid = true;
	if($addr!='http://' && strlen($addr)>0){
		if (!($addr[strlen($addr)-1] == "/")) $addr .= "/";
		$addr = str_replace(eregi_replace("^(.{2,6}://)?[^/]*/", "", $addr), "", $addr);
			if (!preg_match('/^((http|https|ftp):\/\/)?(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\//i', $addr))
			//if (!preg_match('/^(http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\//i', $addr))
				$valid=false;
		}
	return $valid;
}

function IsIP($addr)
{
	if(!preg_match("/[^0-9\.]$/", $addr))
		return TRUE;
	else
		return FALSE;
}

function IsEmail($eml)
{	$valid = true;
	if($eml!='')
	if(preg_match("/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/",$eml) || !(preg_match("/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/",$eml)))
		$valid=false;
			
	return $valid;
}

function IsNotEmpty($field)
{
	if(trim($field)=="")	
		return FALSE;
	else 
		return TRUE;
}

function IsSelected($field)
{
	if(trim($field)=="0")	
		return FALSE;
	else 
		return TRUE;
}

function IsNumber($field)
{
	if(trim($field)!="")	
		return is_numeric($field);	
	else 
		return true;
}

function IsMoney($field)
{
	$valid = true;
	$flen = strlen(trim($field));
	if($flen>0)
	{
		$allowed = "0123456789()-,.$";
		$digits = "0123456789";
		$numCount=0;
		for($i=0;$i<$flen;$i++)
		{
			if(!(strstr($allowed,substr($field,$i,1))))
				$valid=false;
			if(strstr($digits,substr($field,$i,1)))
				$numCount++;	
		}
		if($valid && $numCount==0)
			$valid=false;
	}		
	return $valid;
}

function IsPercent($field)
{
	$valid = true;
	$flen = strlen(trim($field));
	if($flen>0)
	{
		$allowed = "0123456789,.%";
		$digits = "0123456789";
		$numCount=0;
		for($i=0;$i<$flen;$i++)
		{
			if(!(strstr($allowed,substr($field,$i,1))))
				$valid=false;
			if(strstr($digits,substr($field,$i,1)))
				$numCount++;	
		}
		if($valid && $numCount==0)
			$valid=false;
	}	
	return $valid;
}

function IsUSPhone($val) 
{
		$matchArr2 = preg_match("/^\d{10}$/",$val);
		$matchArr3 = preg_match("/^(\d{3})\-\d{3}-\d{4}$/",$val);
		$status = true;
	
	if ($matchArr2 == null && $matchArr3 == null && IsNotEmpty($val)) 
		$status = false;
	return $status;
}


function IsASTPhone($val) 
{
	$matchArr = preg_match("/^\d{8}$/",$val);
	$status = true;
	
	if ($matchArr == null && IsNotEmpty($val))
		$status = false;
	
	return $status;
}

function IsAreaCode($val) 
{
	$matchArr = preg_match("/^\d{2}$/",$val);
	$status = true;
	
	if ($matchArr == null && IsNotEmpty($val))
		$status = false;
	
	return $status;
}

function IsPostCode($val) 
{
	$matchArr = preg_match("/^\d{4}$/",$val);
	$status = true;
	
	if ($matchArr == null && IsNotEmpty($val))
		$status = false;
	
	return $status;
}

function IsColor($color)
{
	$status = true;
	
	if($color != "")
	{
		if($color[0]=='#')
		{
			if(strlen($color)==7)
			{
				$color = "0x".substr($color,1,strlen($color));
				if(!is_numeric($color))
					$status = false;
			}
			else
				$status = false;
		}
	}
	else
		$status = false;
	
	return $status;
}

//Form field check functions
function checkCheckBox($field)
{
	if(!isset($field))	
		return "'N'";
	else 
		return "'Y'";
}

function checkEmptyStr($field)
{
	if($field=="")	
		return 'NULL';
	else 
		return "'".$field."'";
}

function checkEmptyNum($field)
{
	if($field=="")	
		return 'NULL';
	else 
		return $field;
}

function strip_out($val)
{
	$search = array(",","(",")","-","$","%"," ");
	$replace = array("","","","","","","");
	for($i=0;$i<sizeof($search);$i++)
		$val=str_replace($search[$i],$replace[$i],$val);
	return $val;
}
?>